package com.yzz.hzraisedust.hzraseidust.main;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hikvision.sdk.VMSNetSDK;
import com.hikvision.sdk.consts.SDKConstant;
import com.hikvision.sdk.net.bean.LoginData;
import com.hikvision.sdk.net.bean.RootCtrlCenter;
import com.hikvision.sdk.net.bean.SubResourceNodeBean;
import com.hikvision.sdk.net.business.OnVMSNetSDKBusiness;
import com.hikvision.sdk.utils.SDKUtil;
import com.yzz.hzraisedust.hzraseidust.JCApplication;
import com.yzz.hzraisedust.hzraseidust.R;
import com.yzz.hzraisedust.hzraseidust.main.devicedetail.PmFragment;
import com.yzz.hzraisedust.hzraseidust.main.monitorvideo.LiveActivity;
import com.yzz.hzraisedust.hzraseidust.main.squaredetail.SquareDetailInfoActivity;
import com.yzz.hzraisedust.hzraseidust.modelbean.MapItemBean;
import com.yzz.hzraisedust.hzraseidust.modelbean.PmDetailItemBean;
import com.yzz.hzraisedust.hzraseidust.modelbean.PmItemBean;
import com.yzz.hzraisedust.hzraseidust.modelbean.RequstItemBean;
import com.yzz.hzraisedust.hzraseidust.modelbean.VideoBean;
import com.yzz.hzraisedust.hzraseidust.util.Constant;
import com.yzz.hzraisedust.hzraseidust.util.Constants;
import com.yzz.hzraisedust.hzraseidust.util.DemoApp;
import com.yzz.hzraisedust.hzraseidust.util.JsonUtil;
import com.yzz.hzraisedust.hzraseidust.util.NetUtil;
import com.yzz.hzraisedust.hzraseidust.util.UIUtils;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class DeviceDetailActivity extends BaseActivity implements Response.Listener<String>,Response.ErrorListener{

    private MapItemBean item;

    private VideoBean mVideoBean;
    private FragmentManager mFragmentManager;
    private RequstItemBean<PmDetailItemBean> mPmItems;
    private ImageView mVideoDetail;
    private TextView mTitleView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_detail);
        initLoginingDlg();
        showLoginingDlg();
        initData();
        initView();
      //  initFragment();
    }

    private void initFragment() {
        mFragmentManager = getFragmentManager();
        Fragment fragment = mFragmentManager.findFragmentById(R.id.device_linedetail);
        if(fragment == null){
            PmFragment  pmfragment = new PmFragment();
            pmfragment.setType(true);
            pmfragment.setItem(mPmItems);
           FragmentTransaction transaction =  mFragmentManager.beginTransaction();
           transaction.add(R.id.device_linedetail,pmfragment);
           transaction.commit();
        }
    }

    private void initData() {
        item = (MapItemBean)getIntent().getSerializableExtra(Constant.MAP_ITEM_BEAN);
        //请求PM参数
       requstPM();
        //  请求视频参数


    }
    private void requstPM(){
        MyPMResponse myPMResponse = new MyPMResponse();
        NetUtil netUtil = new NetUtil(DeviceDetailActivity.this);
        Map<String,String> params = new HashMap<>();
        params.put(Constant.UID_LOWER_KEY,Constant.Uid);
        params.put(Constant.SN_UPPER_KEY,item.getSN());
        netUtil.requestData(myPMResponse,myPMResponse, Constant.PMDETAIL_URL,params);
    }


    private void initView() {
        View view = findViewById(R.id.navigation_item_include);

        ImageView imageView = view.findViewById(R.id.back_to_pri_imageview);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        TextView textView = view.findViewById(R.id.title_all_textview);
        textView.setText(item.getName());

        mVideoDetail = findViewById(R.id.video_detail_textview);
        mVideoDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showLoginingDlg();
                NetUtil net = new NetUtil(DeviceDetailActivity.this);
                Map<String,String> params = new HashMap<>();
                params.put(Constant.UID_LOWER_KEY,Constant.Uid);
                params.put(Constant.SN_LOWER_KEY,item.getSN());
                net.requestData(DeviceDetailActivity.this,DeviceDetailActivity.this,Constant.DEVICE_VIDEO_URL,params);
            }
        });

    }

    private void requsetVideo(String code,int cameraId){
        SubResourceNodeBean bean = new SubResourceNodeBean();
        bean.setId(1);
        bean.setSysCode(code);
        bean.setId(cameraId);
        bean.setName(item.getName());
        goLive(bean);
        closeLoginingDlg();
    }
    private void goLive(SubResourceNodeBean subResourceNodeBean) {
        if (subResourceNodeBean != null) {
            Intent it = new Intent(this, LiveActivity.class);
            it.putExtra(Constants.IntentKey.CAMERA, subResourceNodeBean);
            startActivity(it);
        } else {
            UIUtils.showToast(this, R.string.empty);
        }
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        UIUtils.showToast(DeviceDetailActivity.this, "发生错误");
        closeLoginingDlg();
        finish();
    }

    @Override
    public void onResponse(String s) {

        final VideoBean videoBean = (VideoBean)JsonUtil.jsonToObject(s,VideoBean.class);


        String mac = JCApplication.getINSTANCE().getMacAddress();
        //登录设备
        VMSNetSDK.getInstance().Login(Constant.CAN_LOGIN_ADDRESS_VIDEO, Constant.LOGIN_ADDRESS_NAME, Constant.LOGIN_ADDRESS_PASSWORD, mac, new OnVMSNetSDKBusiness() {
            @Override
            public void onFailure() {
                super.onFailure();
                closeLoginingDlg();
            }

            @Override
            public void onStatusCallback(int status) {
                super.onStatusCallback(status);
            }

            @Override
            public void onSuccess(Object obj) {
                // super.onSuccess(obj);
                if (obj instanceof LoginData) {
                    //解析版本号
                    String appVersion = ((LoginData) obj).getVersion();
                    SDKUtil.analystVersionInfo(appVersion);
                    requsetVideo(videoBean.getVideoCode(),videoBean.getCameraId());

                }
            }
        });

    }

    class MyPMResponse implements Response.Listener<String>,Response.ErrorListener{

        @Override
        public void onErrorResponse(VolleyError volleyError) {
            closeLoginingDlg();
        }

        @Override
        public void onResponse(String s) {
            Type type = new TypeToken<RequstItemBean<PmDetailItemBean>>(){}.getType();
            Gson gson = new Gson();
            mPmItems = gson.fromJson(s,type);
            if(mPmItems==null || !mPmItems.isSuccess()){
                closeLoginingDlg();
                Toast.makeText(DeviceDetailActivity.this,"数据获取失败",Toast.LENGTH_LONG).show();
                finish();
                return;
            }
            initFragment();
            closeLoginingDlg();
        }
    }
}
